//
//  UIAlertController+Helper.m
//  ReplyAssistant
//
//  Created by liaozhenming on 2018/11/26.
//  Copyright © 2018 KK. All rights reserved.
//

#import "UIAlertController+Helper.h"

@implementation UIAlertController (Helper)

+ (void)kk_alertShowTitle:(NSString*)title message:(NSString*)message cancelTitle:(nullable NSString*)cancelTitle cancelHandle:(nullable UIAlertControllerCancelHandle)cancelHandle sureTitle:(nullable NSString*)sureTitle sureHandle:(nullable UIAlertControllerSureHandle)sureHandle {
    
    UIAlertController *alertController = [self alertControllerWithTitle: title message: message preferredStyle:UIAlertControllerStyleAlert];
    
    if (cancelTitle) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle: cancelTitle style: UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            if (cancelHandle) {
                cancelHandle();
            }
        }];
        [alertController addAction: cancelAction];
    }
    
    if (sureTitle) {
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle: sureTitle style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (sureHandle) {
                sureHandle();
            }
        }];
        [alertController addAction: sureAction];
    }
    
    UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
    [rootViewController presentViewController: alertController animated: YES completion: nil];
}

+ (void)kk_actionSheetShowTitle:(NSString*)title message:(NSString*)message cancelTitle:(NSString*)cancelTitle cancelHandle:(nullable UIAlertControllerCancelHandle)cancelHandle sureTitle:(NSString*)sureTitle sureHandle:(nullable UIAlertControllerSureHandle)sureHandle otherButtonTitles:(NSArray*)buttonTitles selectedHandle:(UIAlertControllerSelectedHandle)selectedHandle {
    
    UIAlertController *alertController = [self alertControllerWithTitle: title message: message preferredStyle:UIAlertControllerStyleActionSheet];
    
    if (cancelTitle) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle: cancelTitle style: UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            if (cancelHandle) {
                cancelHandle();
            }
        }];
        [alertController addAction: cancelAction];
    }
    
    if (sureTitle) {
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle: sureTitle style: UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            if (sureHandle) {
                sureHandle();
            }
        }];
        [alertController addAction: sureAction];
    }
    
    for (NSInteger index = 0; index < buttonTitles.count; index++) {
        
        UIAlertAction *otherAction = [UIAlertAction actionWithTitle: buttonTitles[index] style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (selectedHandle) {
                selectedHandle(index);
            }
        }];
        [alertController addAction: otherAction];
    }
    
    UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
    [rootViewController presentViewController: alertController animated: YES completion: nil];
}

@end
